package ema.tpjee.banking.facade;

import java.io.Serializable;
import java.util.List;

import view.IListView;
import view.ListView;
import controler.BankControler;
import controler.CustomerControler;
import ema.tpjee.banking.dao.BankApplicationDao;
import ema.tpjee.banking.dao.CustomerDao;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.Customer;

public class SessionFacade implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5958151529267592547L;

	private StringBuffer logBuffer = new StringBuffer();

	private BankApplicationDao bankDao;
	private Bank currentBank;
	
	private CustomerDao customerDao;
	private Customer currentCustomer;

	private BankControler bankControler;

	private CustomerControler customerControler;

	
	protected IListView bankView;
	protected IListView customerView;

	private controler.StartControler startControler;
	

	public SessionFacade() {
		super();
		System.out.println("creating SessionFacade");
	}

	public Bank getCurrentBank() {
		if (currentBank==null){
			System.out.println("********No current bank, should not happen");
			try {
				currentBank = bankDao.getList().iterator().next();	
			} catch (Exception e) {
				System.out.println("*********should not happen");
			}	
		}
		return currentBank;
	}

	public void setCurrentBank(Bank currentBank) {
		this.currentBank = currentBank;
	}

	public BankApplicationDao getBankDao() {
		return bankDao;
	}

	public void setBankDao(BankApplicationDao bankDao) {
		System.out.println(this.getClass().getName()+" setBankDao");
		this.bankDao = bankDao;
		bankDao.setSessionFacade(this);
		if (bankDao.isEmpty())
			bankDao.populate();
		currentBank = bankDao.first();
	}

	public void setCustomerDao(CustomerDao customerDao) {
		System.out.println(this.getClass().getName()+" setCustomerDao");
		this.customerDao = customerDao;
		customerDao.setSessionFacade(this);
		if (customerDao.isEmpty())
		    customerDao.populate();
		//currentCustomer = currentBank.getCustomers().get(0);
		currentCustomer = customerDao.first();
	}
	

	
	public CustomerDao getCustomerDao() {
		return customerDao;
	}



	public StringBuffer getLogBuffer() {
		return logBuffer;
	}

	public String getLog() {
		String result = logBuffer.toString();
		if (result.length() == 0)
			result = "[empty]";
		return result;
	}

	public void setLog(String dumm){
		
	}

	public Customer getCurrentCustomer() {
		if (currentCustomer==null){
			System.out.println("********No current customer, should not happen");
			try {
				currentCustomer = customerDao.getList().iterator().next();	
			} catch (Exception e) {
				System.out.println("*********should not happen");
			}	
		}		
		
		return currentCustomer;
	}

	public void setCurrentCustomer(Customer currentCustomer) {
		this.currentCustomer = currentCustomer;
	}

	public void newBank() {
		currentBank=bankDao.create();
	}

	public void deleteBank() {
		currentBank = bankDao.delete(currentBank);	
	}

	public void findBank(int id) {
		currentBank = bankDao.find(id);
	}

	public List<Customer> getCustomers() {
		return 	currentBank.getCustomers();
	}
	
	public void setCustomers(List dumm){
		
	}

	public controler.BankControler getBankControler() {
		return bankControler;
	}

	public void setBankControler(controler.BankControler bankControler) {
		System.out.println(this.getClass().getName()+" setBankControler");
		this.bankControler = bankControler;
		this.bankControler.setSessionFacade(this);	
	}

	public controler.CustomerControler getCustomerControler() {
		return customerControler;
	}

	public void setCustomerControler(
			controler.CustomerControler customerControler) {
		System.out.println(this.getClass().getName()+" setCustomerControler");
		this.customerControler = customerControler;
		this.customerControler.setSessionFacade(this);
	}

	public IListView getBankView() {
		return bankView;
	}

	public IListView getCustomerView() {
		return customerView;
	}

	public void setBankView(ListView bankView) {
		this.bankView = bankView;
		System.out.println("setting bankView");
	
	}

	public void setCustomerView(ListView customerView) {
		this.customerView = customerView;
		System.out.println("setting customerView");
	}

	public controler.StartControler getStartControler() {
		return startControler;
	}

	public void setStartControler(controler.StartControler startControler) {
		System.out.println(this.getClass().getName()+" setStartControler");
		this.startControler = startControler;
		this.startControler.setSessionFacade(this);
	}
	
	

	

}
